package com.ml.mall.service.tax.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ml.mall.bean.req.tax.TaxAddDto;
import com.ml.mall.bean.req.tax.TaxDelDto;
import com.ml.mall.bean.req.tax.TaxQueryPageDto;
import com.ml.mall.bean.req.tax.TaxUpdDto;
import com.ml.mall.entity.tax.Tax;
import com.ml.mall.enums.CommonEnum;
import com.ml.mall.exception.BizException;
import com.ml.mall.mapper.tax.TaxMapper;
import com.ml.mall.service.tax.TaxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Arrays;

@Service
public class TaxServiceImpl extends ServiceImpl<TaxMapper, Tax> implements TaxService {
    @Autowired
    private TaxMapper taxMapper;

    @Override
    public int addTax(TaxAddDto dto) {
        //查询要更改的信息是否和其他信息重复
        checkTaxName(null, dto.getTaxName());
        //同时只能有一个启用的tax,0禁用,1启用
        checkTaxState(null, dto.getState());
        //执行添加操作
        Tax taxInsert = new Tax();
        //封装
        taxInsert.setTaxName(dto.getTaxName());
        taxInsert.setTaxRate(dto.getTaxRate());
        taxInsert.setState(dto.getState());

        int insert = taxMapper.insert(taxInsert);
        if (insert==0){
            throw new BizException(CommonEnum.INSERT_ERROR);
        }
        return insert;
    }

    /**
     * 检查添加与编辑税收的状态参数信息
     * @param id 要编辑的税率信息的id
     * @param state 要修改为的税率状态
     */
    private void checkTaxState(Long id, Integer state) {
        QueryWrapper<Tax> taxQueryWrapper = new QueryWrapper<>();
        taxQueryWrapper.eq("state", 1)
                       .ne(id!=null, "id", id);;
        Integer count = taxMapper.selectCount(taxQueryWrapper);
        if (count + state > 1){
            throw new BizException(CommonEnum.TAX_MORE_THAN_ONE);
        }
    }

    /**
     * 检查添加与编辑税收的名称参数信息
     * @param taxName
     * @param id 要修改的税率id
     */
    private void checkTaxName(Long id, String taxName) {
        QueryWrapper<Tax> taxQueryWrapper = new QueryWrapper<>();
        taxQueryWrapper.eq("tax_name",taxName);
        if(null != id){
            taxQueryWrapper.ne(id!=null, "id", id);
        }
        int selectCount = taxMapper.selectCount(taxQueryWrapper);
        if (selectCount!=0){
            throw new BizException(CommonEnum.ALREADY_EXIST_SAME);
        }
    }

    @Override
    public int updTax(TaxUpdDto dto) {
        //查询要更改的信息是否和其他信息重复
        checkTaxName(dto.getId(), dto.getTaxName());
        //同时只有一个启用的tax
        checkTaxState(dto.getId(), dto.getState());
        //执行更新操作
        Tax tax = new Tax();
        //封装
        tax.setId(dto.getId());
        tax.setTaxName(dto.getTaxName());
        tax.setTaxRate(dto.getTaxRate());
        tax.setState(dto.getState());
        int updateById = taxMapper.updateById(tax);
        if (updateById==0){
            throw new BizException(CommonEnum.UPDATE_ERROR);
        }
        return updateById;
    }

    @Override
    public com.ml.mall.bean.page.Page<Tax> queryByPage(TaxQueryPageDto dto) {
        com.ml.mall.bean.page.Page<Tax> taxPageRes = new com.ml.mall.bean.page.Page<>(dto.getPageIndex(),dto.getPageNum());
        //执行分页查询
        QueryWrapper<Tax> taxQueryWrapper = new QueryWrapper<>();
        taxQueryWrapper.like(dto.getTaxName()!=null,"tax_name",dto.getTaxName())
                .eq(dto.getState()!=null,"state",dto.getState());
        IPage<Tax> page = new Page<>(dto.getPageIndex(),dto.getPageNum());
        page = taxMapper.selectPage(page, taxQueryWrapper);

        taxPageRes.setTotal((int)page.getTotal());
        taxPageRes.setRecords(page.getRecords());
        return taxPageRes;
    }

    @Override
    public int delTax(TaxDelDto dto) {
        //实现删除税务信息
        int i = taxMapper.deleteById(dto.getId());
        if (i==0){
            throw new BizException(CommonEnum.DELETE_ERROR);
        }
        return i;
    }

    @Override
    public BigDecimal getCurTax() {
        QueryWrapper<Tax> wrapper = new QueryWrapper<>();
        wrapper.eq("state",1);
        Tax tfone = taxMapper.selectOne(wrapper);
        BigDecimal td = BigDecimal.ZERO;
        if(tfone!=null){
            td = tfone.getTaxRate();
        }
        return td;
    }
}
